package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.service.ReportDataService;
import com.itheima.service.SetMealService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@ResponseBody
@RequestMapping("/report")
public class ReportController {

    @Reference
    private SetMealService setMealService;
    @Reference
    private ReportDataService reportDataService;

    @RequestMapping("/getSetMealReport")//结合预约订单套餐预定的数量占比
    public Result getSetMealReport(){
        boolean flag=false;
        String message=null;
        Map map= null;
        try {
            map = setMealService.getSetMealReport();
            flag=true;
            message= MessageConstant.GET_SETMEAL_COUNT_REPORT_SUCCESS;
        } catch (Exception e) {
            message= MessageConstant.GET_SETMEAL_COUNT_REPORT_FAIL;
            e.printStackTrace();
        }
        return new Result(flag,message,map);
    }

    @RequestMapping("/getBusinessReportData")
    public Result getBusinessReportData(){
        //查的数据多且杂，建一个新的service，专门查这类数据
        try {
            Map map= reportDataService.getBusinessReportData();
            return new Result(true,MessageConstant.GET_BUSINESS_REPORT_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_BUSINESS_REPORT_FAIL);
        }
    }

    @RequestMapping("/exportBusinessReport")
    public Result exportBusinessReport(HttpServletResponse response, HttpServletRequest request){
        try {
            Map map= reportDataService.getBusinessReportData();
            ServletOutputStream outputStream = response.getOutputStream();

            //            下载需要设置响应头，告诉浏览器是下载，他就会进行下载步骤
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("content-Disposition", "attachment;filename=report.xlsx");

            //获取模板的路径
            String filePath=request.getSession().getServletContext().getRealPath("template")+File.separator+"report_template.xlsx";
            List<Map> mapList = (List<Map>) map.get("hotSetmeal");
            ExcelWriter excelWriter = EasyExcel.write(outputStream).withTemplate(filePath).build();

            /*ExcelWriter excelWriter = EasyExcel.write(fileName).withTemplate(templateFileName).build();
            WriteSheet writeSheet = EasyExcel.writerSheet().build();
            // 这里注意 入参用了forceNewRow 代表在写入list的时候不管list下面有没有空行 都会创建一行，然后下面的数据往后移动。默认 是false，会直接使用下一行，如果没有则创建。
            // forceNewRow 如果设置了true,有个缺点 就是他会把所有的数据都放到内存了，所以慎用
            // 简单的说 如果你的模板有list,且list不是最后一行，下面还有数据需要填充 就必须设置 forceNewRow=true 但是这个就会把所有数据放到内存 会很耗内存
            // 如果数据量大 list不是最后一行 参照下一个
            FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();
            excelWriter.fill(data(), fillConfig, writeSheet);
            excelWriter.fill(data(), fillConfig, writeSheet);
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("date", "2019年10月9日13:28:28");
            map.put("total", 1000);
            excelWriter.fill(map, writeSheet);
            excelWriter.finish();*/
            excelWriter.fill(map,EasyExcel.writerSheet().build());
            excelWriter.fill(mapList,EasyExcel.writerSheet().build());
            excelWriter.finish();
            outputStream.close();
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DOWNLOAD_ORDERSETTING_FAIL);
        }
    }
}
